package com.service.gmall.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.service.gmall.common.result.Result;
import com.service.gmall.model.product.SkuInfo;
import com.service.gmall.model.product.SpuImage;
import com.service.gmall.model.product.SpuInfo;
import com.service.gmall.product.service.SpuImageService;
import com.service.gmall.product.service.SpuInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

/**
 * @author alpha
 * @className: SkuController
 * @date 2022/8/27 10:05
 * @Description
 */
@RestController
@RequestMapping("/admin/product")
public class SpuController {
    @Autowired
    SpuInfoService spuInfoService;
    @Autowired
    SpuImageService spuImageService;
    /**
     * @param page pagenum
     * @param limit pagesize
     * @param category3Id 三级分类ID
     * @description: 获取sku分页列表
     * @return: com.service.gmall.common.result.Result
     * @author: alpha
     * @date: 2022/8/27 10:16
     */
//    admin/product/{page}/{limit}?category3Id=61
    @GetMapping("/{page}/{limit}")
    public Result getSpuPage(@PathVariable("page")Long page,
                             @PathVariable("limit")Long limit,
                             @PathParam("category3Id") Long category3Id){
        Page<SpuInfo> infoPage = new Page<>(page,limit);
        Page<SpuInfo> spuInfoPage = spuInfoService.page(infoPage, new LambdaQueryWrapper<SpuInfo>()
                .eq(SpuInfo::getCategory3Id, category3Id));
        return Result.ok(spuInfoPage);
    }

//    saveSpuInfo

    /**
     * 保存SPU信息
     * @param info :前台传过来的参数
     * @return
     */
    @PostMapping("/saveSpuInfo")
   public Result saveSpuInfo(@RequestBody SpuInfo info){

        spuInfoService.saveSpuInfo(info);
        return Result.ok();
   }
//   /spuImageList/{spuId}

    /**
     * 根据spuId获取图片列表
     * @param spuId
     * @return
     */
    @GetMapping("/spuImageList/{spuId}")
    public Result getSpuImageList(@PathVariable("spuId")Long spuId){
        List<SpuImage> images = spuImageService.list(new LambdaQueryWrapper<SpuImage>()
                .eq(SpuImage::getSpuId, spuId));
        return Result.ok(images);
    }


}
